﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading;
using System.Diagnostics;

namespace SpaceArcade
{
    class GameScene : Scene
    {
            

        
        static Random rand;
       
        public Player myplayer;
        public bool DrawLevelNumber = true;

        public List<Rock> Rocks
        {
            get;
            protected set;
        }

        public static int Score
        {
            get;
            protected set;
        }

        public delegate void FallHandler();
        public static event FallHandler OnFall;

        public delegate void SaveResultHandler();
        public static event SaveResultHandler OnSave;

        

        public GameScene(Background bg, Form f)
            :base(bg, f)
        {
            //this.Background = bg;            
            this.Rocks = new List<Rock>();
            //this.GameForm = f;
            this.gameBM = new Bitmap(GameForm.Width, GameForm.Height);
            rand = new Random();
            
            this.PowerUps = new List<PowerUP>();
            Rocks.Add(new Rock(50, -1500));
            Rocks.Add(new Rock(150, -1500, "strip_rock_type_B.png"));
            Rocks.Add(new Rock(250, -1500, "strip_rock_type_C.png"));
            Rocks.Add(new Rock(350, -1500, "strip_rock_type_D.png"));
            Rocks.Add(new Rock(450, -1500));
            Rocks.Add(new Rock(550, -1500, "strip_rock_type_B.png"));

            for (int i = 0; i < Rocks.Count; i++)
            {
                Rocks[i].SetFramesRange(0, 15);
            }

            myplayer = new Player(300, GameForm.Height - 150);
        }

        public GameScene() { }

       

        public override void SetBackGround(Background bg)
        {
            this.Background = bg;
        }

        //public void AddRock(Rock obj)
        //{
        //    this.Rocks.Add(obj);              
        //}

        public List<PowerUP> PowerUps
        {
            get;
            set;
        }
       

        public void OnKeyDown(KeyEventArgs e)
        {
            myplayer.OnKeyDown(e);
        }

        public void OnKeyUp(KeyEventArgs e)
        {
            myplayer.OnKeyUp(e);
        }

        

        public virtual void Update(EventArgs e, System.Windows.Forms.Timer timer1)
        {
            //Обробка пострілу
            Bomb temp = null;
            Bomb tempOut = null;
            if (myplayer != null)
            {
                myplayer.Move(GameForm);
                for (int i = 0; i < myplayer.Bombs.Count; i++)
                {
                    myplayer.Bombs[i].Move();
                    foreach (Rock rock in Rocks)
                    {
                        if (myplayer.Bombs[i].Intersects(rock))
                        {
                            rock.X = rand.Next(GameForm.Width - 50);
                            rock.Y = -200;
                            temp = myplayer.Bombs[i];
                            if (OnFall != null)
                                OnFall();
                        }

                    }
                    if (myplayer.Bombs[i].Y < -50)
                    {
                        tempOut = myplayer.Bombs[i];
                    }
                }
                if (temp != null)
                {
                    if (!myplayer.PowerUpedBlast)
                    myplayer.Bombs.Remove(temp);
                }
                if (tempOut != null)
                {
                    myplayer.Bombs.Remove(tempOut);
                }
            }
            //Надпис на початку рівня
            for (int i = 0; i < Rocks.Count; i++)
            {
                if (Rocks[i].Y > -50)
                {
                    DrawLevelNumber = false;
                }
            }
            //
            //Падіння астероїдів
            foreach (Rock rock in Rocks)
            {
                if (rock != null)
                {
                    rock.Fall();
                    if (rock.Y > GameForm.Height)
                    {
                        rock.Y = -50;
                        rock.X = rand.Next(gameBM.Width);
                        switch (Game.currentGameLevel)
                        {
                        case 1:
                        rock.speedY = rand.Next(10, 15);                    
                        rock.speedX = rand.Next(-7, 7);
                        break;
                            case 2:
                        rock.speedY = rand.Next(13, 20);                    
                        rock.speedX = rand.Next(-9, 9);
                        break;
                            case 3:
                        rock.speedY = rand.Next(15, 22);
                        rock.speedX = rand.Next(-12, 12);
                        break;
                        }

                        if (OnFall != null)
                            OnFall();

                        double chance = rand.NextDouble();
                        if (chance <= 0.02)
                        {
                            PowerUps.Add(new PowerUP("powerupBlue_bolt.png", rand.Next(gameBM.Width), -50));
                        }else if (chance > 0.02 && chance <= 0.06)
                        {
                            PowerUps.Add(new Shield("shield_bronze.png", rand.Next(gameBM.Width), -50));
                        }else if(chance > 0.06 && chance <= 0.07)
                        {
                            PowerUps.Add(new BonusLife("pill_red.png", rand.Next(gameBM.Width), -50));
                        }
                    }
                    if (rock.X > GameForm.Width)
                        rock.X = 0;

                    if (rock.X < 0)
                        rock.X = GameForm.Width;
                }                
            }
            
            //Обробка зіткнення з бонусом
            for (int i = 0; i < PowerUps.Count; i++)
            {
                PowerUps[i].Fall();
                if (myplayer.Intersects(PowerUps[i]))
                    PowerUps.RemoveAt(i);
            }
            if (myplayer.spaceShield != null)
            {
                myplayer.spaceShield.X = myplayer.X - 10;
                myplayer.spaceShield.Y = myplayer.Y - 10;
            }
           
            
                //Обробка зіткнень астероїда з гравцем
                if (myplayer != null)
                {
                    for (int i = 0; i < Rocks.Count; i++)
                        if (myplayer.Intersects(Rocks[i]))
                        {
                           
                            
                            Rocks[i].X = rand.Next(GameForm.Width - 50);
                            Rocks[i].Y = -200;
                        }
                    
                    //Кінець гри
                    if (myplayer.HitPoints <= 0)
                    {
                        
                        myplayer.Explode();
                        if (myplayer.stopGame)
                        {
                            timer1.Stop();
                            // MessageBox.Show("Game over, your score = " + Convert.ToString(Score));

                            if (OnSave != null)
                                OnSave();

                            //Application.Exit();

                            //Application.Restart();
                        }
                    }
                   

                }
        }

        
        
        public override void Draw()
        {
                     
            
            this.Background.Draw(gameBM);
           
            foreach (Rock rock in Rocks)
            {
                rock.Draw(gameBM);
            }
            for (int i = 0; i < PowerUps.Count; i++)
            {
                PowerUps[i].Draw(gameBM);
            }
            if (myplayer != null)
            {
                myplayer.Draw(gameBM);

                Graphics.FromImage(gameBM).DrawString(String.Format("HP:{0}", myplayer.HitPoints), new Font("Cooper black", 40), Brushes.White, new Point(500, 30));
            }
            
            if(DrawLevelNumber)
            {
                Graphics.FromImage(gameBM).DrawString(String.Format("Level {0}", Game.currentGameLevel), new Font("Cooper black", 60), Brushes.White, new Point(200, 250));
            }

           
               
        }
    }
}
